﻿/// ----------------------------------------------------------------------------------
///         Check-In Notifier : System tray notifier for TFS check-in events
/// ----------------------------------------------------------------------------------
///                 Copyright © 2010 Fatih Boy
///               http://www.enterprisecoding.com
/// ---------------------------------------------------------------------------------- 
///  File Name          : NotificationForm.cs
///  Developer          : Fatih Boy (fatih@enterprisecoding.com)
/// ----------------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Com.Enterprisecoding.TFS.CheckInNotifier.EventData;

namespace Com.Enterprisecoding.TFS.CheckInNotifier {
    internal partial class NotificationDialog : Form {
        private Timer windowCloseTimer = new Timer();
        private CheckinEvent checkinEvent;

        public NotificationDialog(CheckinEvent checkinEvent) {
            this.checkinEvent = checkinEvent;

            InitializeComponent();

            Text = checkinEvent.Title;
            windowCloseTimer.Tick += windowCloseTimer_Tick;
            windowCloseTimer.Interval = 20000;

            title.Text = string.Format("Check-In for project '{0}' by {1}", checkinEvent.TeamProject, checkinEvent.Committer);

            commiter.Text = checkinEvent.Committer;
            changeset.Text = checkinEvent.Number.ToString();
            eventDate.Text = checkinEvent.CreationDate;
            comment.Text = checkinEvent.Comment;

            foreach (var artifact in checkinEvent.Artifacts) {
                if (artifact.ArtifactType=="Changeset") {
                    showChangeset.Tag = artifact.Url;
                    showChangeset.Enabled = true;

                    break;
                }
            }

            listFiles.Enabled = (showChangeset.Enabled && checkinEvent.Artifacts.Length > 1) || (!showChangeset.Enabled && checkinEvent.Artifacts.Length > 0);
        }

        protected override bool ShowWithoutActivation {
            get { return true; }
        }

        protected override void OnShown(EventArgs e) {
            base.OnShown(e);

            var screenWorkingArea = Screen.GetWorkingArea(Screen.PrimaryScreen.Bounds);

            Top = screenWorkingArea.Bottom - Height - 11;
            Left = screenWorkingArea.Width - Width - 11;

            windowCloseTimer.Start();
        }

        protected void windowCloseTimer_Tick(object sender, System.EventArgs e) {
            Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
            base.OnClosing(e);

            windowCloseTimer.Stop();
        }

        private void listFiles_Click(object sender, EventArgs e) {
            windowCloseTimer.Stop();
            new FileListingDialog(checkinEvent.Artifacts).ShowDialog(this);
            windowCloseTimer.Start();
        }

        private void showChangeset_Click(object sender, EventArgs e) {
            windowCloseTimer.Stop();
            new BrowserDialog(showChangeset.Tag as string).ShowDialog(this);
            windowCloseTimer.Start();
        }

        private void NotificationForm_MouseEnter(object sender, EventArgs e) {
            windowCloseTimer.Enabled = false;
        }

        private void NotificationForm_MouseLeave(object sender, EventArgs e) {
            windowCloseTimer.Enabled = true;
        }
    }
}